package edu.npu.cv.controller.rest;

import edu.npu.cv.constant.ResultCode;
import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.constant.URLConstant;

import edu.npu.cv.entity.MemberDO;
import edu.npu.cv.entity.PageDO;
import edu.npu.cv.entity.PublicationDO;
import edu.npu.cv.entity.ResultDO;
import edu.npu.cv.enums.Degree;
import edu.npu.cv.service.ArticleService;
import edu.npu.cv.service.MemberService;
import edu.npu.cv.service.PublicationService;
import edu.npu.cv.utils.PageQueryUtil;
import edu.npu.cv.vo.ArticleVO;
import edu.npu.cv.vo.MemberVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.util.Map;

/**
 * @ClassName AdminRestController
 * @Description TODO
 * @Author Quentin_zyj
 * @Date 2021/10/8 20:25
 */
@RestController
@RequestMapping(URLConstant.S+URLConstant.ADMIN_REST)
public class AdminRestController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private PublicationService publicationService;

    @GetMapping(URLConstant.ADMIN_REST_GET_MEMBERS)
    public ResultDO<PageDO> getMembers(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null) {
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return memberService.findAll(new PageQueryUtil(params));
    }

    @PostMapping(URLConstant.ADMIN_REST_SAVE_MEMBER)
    public ResultDO<Void> saveMember(@RequestBody MemberVO memberVO) {
        // 数据过筛-姓名必填、学位必须合法
        if (StringUtils.isBlank(memberVO.getMemberName()) || StringUtils.isBlank(memberVO.getDegree())
            || !Degree.isLegal(memberVO.getDegree())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return memberService.saveMember(MemberDO.transMemberVO2DO(memberVO));
    }

    @PostMapping(URLConstant.ADMIN_REST_UPDATE_MEMBER)
    public ResultDO<Void> updateMember(@RequestBody MemberVO memberVO) {
        // 数据过筛-姓名必填、学位必须合法、id必须合法
        if (memberVO.getMemberId() <= 0 || StringUtils.isBlank(memberVO.getMemberName())
            || StringUtils.isBlank(memberVO.getDegree())
            || !Degree.isLegal(memberVO.getDegree())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return memberService.updateMember(MemberDO.transMemberVO2DO(memberVO));
    }

    @PostMapping(URLConstant.ADMIN_REST_DELETE_MEMBER)
    public ResultDO<Void> deleteByIds(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return memberService.deleteBatch(ids);
    }

    @GetMapping(URLConstant.ADMIN_REST_GET_ARTICLES)
    public ResultDO<PageDO> getArticles(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null) {
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return articleService.findAll(new PageQueryUtil(params));
    }

    @PostMapping(URLConstant.ADMIN_REST_DELETE_ARTICLE)
    public ResultDO<Void> deleteArticlesByIds(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return articleService.deleteByIds(ids);
    }

    @PostMapping(URLConstant.ADMIN_REST_SAVE_ARTICLE)
    public ResultDO<Void> saveArticle(@RequestBody ArticleVO articleVO) {
        return articleService.saveArticle(articleVO);
    }

    @PostMapping(URLConstant.ADMIN_REST_UPDATE_ARTICLE)
    public ResultDO<Void> updateArticle(@RequestBody ArticleVO articleVO) {
        if (articleVO.getArticleId() <= 0) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return articleService.updateArticle(articleVO);
    }

    @PostMapping(URLConstant.ADMIN_REST_DELETE_PUBLICATION)
    public ResultDO<Void> deletePublicationByIds(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return publicationService.deleteByIds(ids);
    }

    @PostMapping(URLConstant.ADMIN_REST_SAVE_PUBLICATION)
    public ResultDO<Void> savePublication(@RequestBody PublicationDO publicationDO) {
        return publicationService.save(publicationDO);
    }

    @PostMapping(URLConstant.ADMIN_REST_UPDATE_PUBLICATION)
    public ResultDO<Void> updatePublication(@RequestBody PublicationDO publicationDO) {
        if (publicationDO.getId() <= 0L) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return publicationService.update(publicationDO);
    }

}
